package study.itheima.controller;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import study.itheima.domain.User;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//测试
public class TestDemo {
    private InputStream is;
    private SqlSession sqlSession;
    //代码运行之前执行的代码
    @Before
    public void before() throws IOException {
        //读取配置文件
        is = Resources.getResourceAsStream("coreconfig.xml");
        //构建SqlSessionFactory对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        //获取SqlSession对象
        sqlSession = build.openSession();
    }
    //代码运行之后执行的代码
    @After
    public void after() throws IOException {
        //提交事务,关流
        sqlSession.commit();
        sqlSession.close();
        is.close();
    }
    //查询所有
    @Test
    public void findAllDemo() throws IOException {
        List<User> list = sqlSession.selectList("one.findAll");
        Date dd = list.get(0).getBirthday();
        System.out.println("dd = " + dd);
        System.out.println("list = " + list);
    }
    //查询通过id
    @Test
    public void findByIdDemo() throws IOException {
        User user = sqlSession.selectOne("one.findById", 1);
        System.out.println("user = " + user);
    }
    //统计人员数量
    @Test
    public void countDemo() throws IOException {
        int num = sqlSession.selectOne("one.count");
        System.out.println(num);
    }
    //统计男女数量
    @Test
    public void sexcountDemo() throws IOException {
        List<Map> list= sqlSession.selectList("one.sexcount");
        Map sexMap=new HashMap();
        for (Map map1 : list) {
            String sex = (String) map1.get("sex");
            Long total = (Long) map1.get("count(*)");
            //System.out.println(sex+"--"+total);
            sexMap.put(sex,total);
        }
        System.out.println(sexMap);
    }
    //更新
    @Test
    public void updateDemo() throws IOException, ParseException {
        User user = sqlSession.selectOne("one.findById", 1);
        Date parse = new SimpleDateFormat("yyyy-MM-dd").parse("1996-08-02");
        user.setBirthday(parse);
        sqlSession.update("one.update",user);
    }
    //根据姓名和年龄查人
    @Test
    public void findbynaDemo() throws IOException{
        Map map = new HashMap();
        map.put("ne","张三");
        map.put("ae",23);
        User user = sqlSession.selectOne("one.findbyna",map);
        System.out.println(user);
    }
    //根据姓名模糊查询
    //占位
    @Test
    public void findlike1Demo() throws IOException {
        List<User> users = sqlSession.selectList("one.findlike1", "%张%");
        System.out.println(users);
    }
    //字符串拼接
    @Test
    public void findlike2Demo() throws IOException {
        List<User> users = sqlSession.selectList("one.findlike2", "张");
        System.out.println(users);
    }
    //取保存后id
    @Test
    public void getidDemo() throws IOException, ParseException {
        User user=new User();
        user.setName("吕布");
        user.setAge(77);
        user.setSex("男");
        Date parse = new SimpleDateFormat("yyyy-MM-dd").parse("556-07-13");
        user.setBirthday(parse);
        System.out.println("前"+user);
        sqlSession.insert("one.getid",user);
        System.out.println("后"+user);
    }
}
